/**
* 基础菜单 商品管理
*/
<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>资金账户</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索筛选 -->
		<el-form :inline="true" :model="formInline" class="user-search">


			<el-form-item>
				<el-button size="medium" type="primary" icon="el-icon-plus" @click="details()">
					添加资金账户(只能拥有一个账户)</el-button>
			</el-form-item>
		</el-form>
		<!--列表-->
		<el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
			element-loading-text="拼命加载中" style="width: 90%;">
			<el-table-column align="center" type="selection" width="60">
			</el-table-column>
			<el-table-column sortable prop="uaccount" label="用户账号" width="150">
			</el-table-column>
			<el-table-column sortable prop="ename" label="企业名称" width="150">
			</el-table-column>
			<el-table-column sortable prop="faccount" label="资金账户编号" width="150">
			</el-table-column>
			<el-table-column sortable prop="residual" label="账户余额" width="150">
			</el-table-column>
			<el-table-column sortable prop="status" label="账户状态" width="150">
			</el-table-column>
			<el-table-column align="center" label="操作" min-width="100">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改密码</el-button>
					<el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页组件 -->
		<Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
		<!-- 添加页面 -->
		<el-dialog title="提示" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
			<el-form label-width="150px" :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="用户账号" prop="uaccount">
					<el-input size="small" v-model="editForm.uaccount" auto-complete="off" placeholder="用户账号" readonly>
					</el-input>
				</el-form-item>
				<el-form-item label="企业名称" prop="ename">
					<el-input size="small" v-model="editForm.ename" auto-complete="off" placeholder="企业名称" readonly>
					</el-input>

				</el-form-item>
				<el-form-item label="账户余额" prop="residual">
					<el-input size="small" v-model="editForm.residual" auto-complete="off" placeholder="账户余额" readonly>
					</el-input>

				</el-form-item>
				<el-form-item label="支付密码" prop="pay_pass">
					<el-input size="small" v-model="editForm.pay_pass" auto-complete="off" placeholder="请输入原密码"
						show-password>
					</el-input>
				</el-form-item>
				<el-form-item label="确认支付密码" prop="newpay_pass">
					<el-input size="small" v-model="editForm.newpay_pass" auto-complete="off" placeholder="确认支付密码"
						show-password>
					</el-input>
				</el-form-item>
			</el-form>

			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="submit()">确 定 添 加</el-button>
			</span>
		</el-dialog>
		<!-- 编辑界面 -->
		<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
			<el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="原密码" prop="changepay_pass">
					<el-input size="small" v-model="editForm.changepay_pass" auto-complete="off" placeholder="请输入原密码"
						show-password>
					</el-input>
				</el-form-item>
				<el-form-item label="新密码" prop="newpay_pass">
					<el-input size="small" v-model="editForm.newpay_pass" auto-complete="off" placeholder="请输入新密码"
						show-password>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">
					确定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		deptList,
		deptSave,
		deptDelete
	} from '../../api/userMG'
	import Pagination from '../../components/Pagination'
	import axios from '../../api/axiosFun.js'
	export default {
		data() {
			return {
				//上传的文件
				dialogVisible: false, //添加页面
				nshow: true, //switch开启
				fshow: false, //switch关闭
				loading: false, //是显示加载
				editFormVisible: false, //控制编辑页面显示与隐藏
				restaurants: [],
				title: '添加',
				editForm: {
					eid: '',
					id: '',
					uaccount: '',
					ename: '',
					residual: '',
					status: '',
					faccount: '',
					pay_pass: '',
					newpay_pass: '',
					changepay_pass: '',
					token: localStorage.getItem('logintoken')
				},
				// rules表单验证
				rules: {
					deptName: [{
						required: true,
						message: '请输入部门名称',
						trigger: 'blur'
					}],
					deptNo: [{
						required: true,
						message: '请输入部门代码',
						trigger: 'blur'
					}]
				},
				formInline: {
					page: 1,
					limit: 8,
					varLable: '',
					varName: '',
					user: JSON.parse(localStorage.getItem("userdata")),
					token: localStorage.getItem('logintoken')
				},
				// 删除部门
				seletedata: {
					ids: '',
					token: localStorage.getItem('logintoken')
				},
				userparm: [], //搜索权限
				listData: [], //用户数据
				// 分页参数
				pageparm: {
					currentPage: 1,
					pageSize: 5,
					total: 10
				}
			}
		},
		// 注册组件
		components: {
			Pagination
		},
		/**
		 * 数据发生改变
		 */

		/**
		 * 创建完毕
		 */
		created() {
			this.getdata()
			//如果roleid为1 表示是平台管理员 可显示所有企业的资金账户
			if (2 > 1) {
				console.log(2)
			}
		},
		/**
		 * 里面的方法只有被调用才会执行
		 */
		methods: {
			//文件上传
			submitUpload() {
				this.$refs.upload.submit();
			},
			handleRemove(file, fileList) {
				console.log(file, fileList);
			},
			handlePreview(file) {
				console.log(file);
			},
			//联想搜索
			querySearch(queryString, cb) {
				var restaurants = this.restaurants;
				var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
				// 调用 callback 返回建议列表的数据
				cb(results);
			},
			createFilter(queryString) {
				return (restaurant) => {
					return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
				};
			},

			handleSelect(item) {
				console.log(item);
			},


			// 获取公司列表
			getdata() {
				this.loading = true
				/***
				 * 调用接口，注释上面模拟数据 取消下面注释
				 */
				/* this.$axios.get('/api/etmsUser/findStudentByTrain/' + this.formInline.currentPage + '/' + this.formInline
						.pageSize + '/' + this.tid)*/
				this.$axios.post("api/faccount/findID/" +this.formInline.user.enterprise_id)
					.then(res => {
						this.loading = false
						console.log(res)
						if (res.data.code == 500) {
							this.$message({
								type: 'info',
								message: res.data.msg
							})
						} else {
							this.listData = res.data.data
							// 分页赋值
							this.pageparm.currentPage = res.data.data.pageNum
							this.pageparm.pageSize = res.data.data.pageSize
							this.pageparm.total = res.data.data.total

						}
					})
					.catch(err => {
						this.loading = false
						this.$message.error('菜单加载失败，请稍后再试！')
					})
			},
			// 分页插件事件 

			callFather(parm) {
				this.formInline.page = parm.currentPage
				this.formInline.limit = parm.pageSize
				this.getdata(this.formInline)
			},
			// 显示添加
			details: function() {
				if(this.listData!=''&&this.listData!='undefined'){
					this.dialogVisible = false
					this.$message({
						type: 'error',
						message: '已经有一个账户了'
					})
				}else{
					this.dialogVisible = true
					this.editForm.pay_pass=''
					this.editForm.newpay_pass=''
					this.editForm.uaccount=this.formInline.user.name
					this.editForm.eid=this.formInline.user.enterprise_id
					this.title = '资金账户添加'
				}
			},
			//确定添加
			submit() {
				if (parseInt(this.editForm.newpay_pass) != parseInt(this.editForm.pay_pass)) {
					this.$message.error('密码输入错误，请稍后再试！')
				}else{
					this.$axios.post("api/faccount/add/",this.editForm).then(res=>{
						if(res.data.code==200){
							this.$message.success("添加成功")
							this.getdata()
							this.dialogVisible=false
						}else{
							this.$message.error("添加失败")
						}
					}).catch()
				}
			},
			//显示编辑界面
			handleEdit: function(index, row) {
				this.editFormVisible = true
				if (row != undefined && row != 'undefined') {
					this.title = '修改密码'
					this.editForm.newpay_pass = ''
					this.editForm.changepay_pass = ''
					this.editForm.pay_pass = row.pay_pass
					this.editForm.id = row.id
				}

			},
			submitForm(editData) {
				if (this.editForm.id != '' && this.editForm != undefined) {
					if (parseInt(this.editForm.changepay_pass) === parseInt(this.editForm.pay_pass)) {
						this.$axios.post("api/faccount/update/" + this.editForm.newpay_pass + "/"+this.editForm.id).then(res => {
							this.editFormVisible = false
							this.loading = false
							if (res.data.code == 200) {
								this.getdata()
								this.$message({
									type: 'success',
									message: '修改成功！'
								})
							} else {
								this.$message({
									type: 'info',
									message: res.data.msg
								})
							}
						}).catch(err => {
							this.editFormVisible = false
							this.loading = false
							this.$message.error('修改失败，请稍后再试！')
						})
					} else {
						this.$message.error('原密码错误，请稍后再试！')
					}


				}

			},
			// 删除公司
			deleteUser(index, row) {
				this.$confirm('确定要删除吗?', '信息', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$axios.post("api/faccount/del/" + row.id)
							.then(res => {
								if (res.data.code == 200) {
									this.$message({
										type: 'success',
										message: '已删除!'
									})
									this.getdata()
								} else {
									this.$message({
										type: 'info',
										message: res.data.msg
									})
								}
							})
							.catch(err => {
								this.loading = false
								this.$message.error('删除失败，请稍后再试！')
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 关闭编辑、增加弹出框
			closeDialog() {
				this.editFormVisible = false
			}
		}
	}
</script>

<style scoped>
	.user-search {
		margin-top: 20px;
	}

	.userRole {
		width: 100%;
	}
</style>
